<?php

namespace Home\Controller;

use Think\Controller;
use Home\Model\CategoryModel;
use Home\Model\SiteModel;
use Home\Model\ArticleModel;
use Home\Model\TagModel;
use Home\Model\SearchLogModel;

class SitemapController extends Controller {
	public function _initialize() {
		$this->num = 49000;
	}
	public function index() {
		echo 'sitemap';
	}
	public function category() {
		header ( 'Content-Type: text/xml' );
		$category = new CategoryModel ();
		$result = $category->search ();
		$xml = '<?xml  version="1.0" encoding="utf-8"?><urlset>';
		foreach ( $result ['list'] as $val ) {
			$xml .= '<url><loc>' . $val ['url'] . '</loc><changefreq>daily</changefreq><priority>1</priority></url>';
		}
		$xml .= '</urlset>';
		echo $xml;
	}
	public function site() {
		header ( 'Content-Type: text/xml' );
		$site = new SiteModel ();
		$result = $site->search ( 1, 1, 'updatetime desc', $this->num );
		$xml = '<?xml  version="1.0" encoding="utf-8"?><urlset>';
		foreach ( $result ['list'] as $val ) {
			$xml .= '<url><loc>' . $val ['url'] . '</loc><lastmod>' . $val ['updatetime'] . '</lastmod></url>';
		}
		$xml .= '</urlset>';
		echo $xml;
	}
	public function article() {
		header ( 'Content-Type: text/xml' );
		$article = new ArticleModel ();
		$result = $article->search ( 1, 1, 'updatetime desc', $this->num );
		$xml = '<?xml  version="1.0" encoding="utf-8"?><urlset>';
		foreach ( $result ['list'] as $val ) {
			$xml .= '<url><loc>' . $val ['url'] . '</loc></url>';
		}
		$xml .= '</urlset>';
		echo $xml;
	}
	public function tag() {
		header ( 'Content-Type: text/xml' );
		$tag = new TagModel ();
		$result = $tag->getHotTag ( $this->num );
		$xml = '<?xml  version="1.0" encoding="utf-8"?><urlset>';
		foreach ( $result ['list'] as $val ) {
			if ($val ['url']) {
				$xml .= '<url><loc>' . str_replace ( ' ', '', $val ['url'] ) . '</loc></url>';
			}
		}
		$xml .= '</urlset>';
		echo $xml;
	}
	public function searchkey() {
		header ( 'Content-Type: text/xml' );
		$search = new SearchLogModel ();
		$result = $search->search ( 'num>0', 1, 'createtime desc', $this->num, 'keyword' );
		$xml = '<?xml  version="1.0" encoding="utf-8"?><urlset>';
		foreach ( $result ['list'] as $val ) {
			$xml .= '<url><loc>' . $val ['url'] . '</loc></url>';
		}
		$xml .= '</urlset>';
		echo $xml;
	}
}